/**
 * 
 */
package de.mbenning.fsim.web.controller;

import java.beans.PropertyEditor;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.support.RequestContextUtils;

import de.mbenning.fsim.domain.Kontinent;
import de.mbenning.fsim.domain.Land;
import de.mbenning.fsim.domain.Verein;
import de.mbenning.fsim.persistence.dao.interfaces.IMainDAO;
import de.mbenning.fsim.persistence.dao.interfaces.IVereinDAO;
import de.mbenning.fsim.web.utils.KontinentPropertyEditorSupport;
import de.mbenning.fsim.web.utils.LandPropertyEditorSupport;
import de.mbenning.fsim.web.utils.VereinPropertyEditorSupport;

/**
 * @author Martin.Benning
 *
 */
public class AbstractFsimBaseController extends SimpleFormController {
	
	protected IMainDAO mainDAO;
	
	protected IVereinDAO vereinDAO;

	@Override
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		super.initBinder(request, binder);
		
		NumberFormat currentNumberFormat = NumberFormat.getNumberInstance(RequestContextUtils.getLocale(request));
        PropertyEditor intEditor = new CustomNumberEditor(Integer.class, currentNumberFormat, true);
        PropertyEditor longEditor = new CustomNumberEditor(Long.class, currentNumberFormat, true);
        PropertyEditor floatEditor = new CustomNumberEditor(Float.class, currentNumberFormat, true);
        PropertyEditor doubleEditor = new CustomNumberEditor(Double.class, currentNumberFormat, true);
        PropertyEditor decimalEditor = new CustomNumberEditor(BigDecimal.class, currentNumberFormat, true);
        PropertyEditor customDateEditor = new CustomDateEditor(new SimpleDateFormat("dd.MM.yyyy"), true);
        PropertyEditor boolEditor = new CustomBooleanEditor(true);
        binder.registerCustomEditor(int.class, intEditor);
        binder.registerCustomEditor(Integer.class, intEditor);
        binder.registerCustomEditor(long.class, longEditor);
        binder.registerCustomEditor(Long.class, longEditor);
        binder.registerCustomEditor(float.class, floatEditor);
        binder.registerCustomEditor(Float.class, floatEditor);
        binder.registerCustomEditor(double.class, doubleEditor);
        binder.registerCustomEditor(Double.class, doubleEditor);
        binder.registerCustomEditor(BigDecimal.class, decimalEditor);
        binder.registerCustomEditor(Date.class, customDateEditor);
        binder.registerCustomEditor(boolean.class, boolEditor);
        binder.registerCustomEditor(Boolean.class, boolEditor);
        
        binder.registerCustomEditor(Land.class, new LandPropertyEditorSupport(mainDAO));
		binder.registerCustomEditor(Kontinent.class, new KontinentPropertyEditorSupport(mainDAO));
		binder.registerCustomEditor(Verein.class, new VereinPropertyEditorSupport(vereinDAO));
	}

	public IMainDAO getMainDAO() {
		return mainDAO;
	}

	public void setMainDAO(IMainDAO mainDAO) {
		this.mainDAO = mainDAO;
	}

	public IVereinDAO getVereinDAO() {
		return vereinDAO;
	}

	public void setVereinDAO(IVereinDAO vereinDAO) {
		this.vereinDAO = vereinDAO;
	}

}
